package com.ruoyi.web.controller.zkcontroller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.web.domain.Category;
import com.ruoyi.web.service.zkService.CategoryService;
import com.ruoyi.web.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){

        log.info("category:{}",category);
        categoryService.save(category);
        return R.ok("新增分类成功");

    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(@RequestParam("page") int page,@RequestParam("pageSize") int pageSize){
        Page pageInfo = new Page(page,pageSize);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件
        queryWrapper.orderByAsc(Category::getSort);
        categoryService.page(pageInfo,queryWrapper);
        return R.ok(pageInfo);
    }

    /**
     * 分类删除
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long id){
        log.info("delete: ",id);
        categoryService.removeById(id);
        return R.ok("分类删除成功");
    }

    /**
     * 分类管理-根据信息修改分类
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category){
        log.info("修改分类信息{}",category);
        categoryService.updateById(category);
        return R.ok("修改成功");
    }

    /**
     * 分类管理——查询菜品分类
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        if(category == null){
            return R.error("传入数据为空");
        }
        List<Category> list = categoryService.lambdaQuery().eq(Category::getType, category.getType()).list();

        return R.ok(list);


//        return null;
    }
}
